<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="username" placeholder="银商名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="userqq" placeholder="银商QQ"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="userWx" placeholder="银商微信"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getUsers(pageSize)">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="vipList" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column prop="id" label="#">
			</el-table-column>
			<el-table-column prop="name" label="银商名称" width="250">
			</el-table-column>
			<el-table-column prop="qq" label="QQ" width="200">
			</el-table-column>
			<el-table-column prop="wx" label="微信" width="300">
			</el-table-column>
			<el-table-column prop="friendliness" label="友好度" width="150">
			</el-table-column>
			<el-table-column label="操作" min-width="200">
				<template slot-scope="scope">
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange"  :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="100px"  :rules="addFormRules" ref="addForm">
				<el-form-item label="银商名称" prop="name">
					<el-input v-model="addForm.name" ></el-input>
				</el-form-item>
				<el-form-item label="QQ" prop="qq">
					<el-input v-model="addForm.qq" type="number"></el-input>
				</el-form-item>
				<el-form-item label="微信" prop="wx">
					<el-input v-model="addForm.wx"></el-input>
				</el-form-item>
				<el-form-item label="友好度" prop="friendly">
					<el-select v-model="addForm.friendliness" placeholder="请选择">
						<el-option
								v-for="item in options"
								:key="item.value"
								:label="item.label"
								:value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
                options: [
                    {value: '1', label: '1'},
                    {value: '2', label: '2'},
                    {value: '3', label: '3'},
                    {value: '4', label: '4'},
                    {value: '5', label: '5'},
                    {value: '6', label: '6'},
                    {value: '7', label: '7'},
                    {value: '8', label: '8'},
                    {value: '9', label: '9'},
                    {value: '10', label: '10'},
                    {value: '11', label: '11'},
                    {value: '12', label: '12'},
                    {value: '13', label: '13'},
                    {value: '14', label: '14'},
                    {value: '15', label: '15'},
                    {value: '16', label: '16'},
                    {value: '17', label: '17'},
                    {value: '18', label: '18'},
                    {value: '19', label: '19'},
                    {value: '20', label: '20'}
                    ],
                uploadImg:[],
                vipList: [],
				total: 0,
				page: 1,
                pageSize:15,
				listLoading: false,
                username:'',
				userqq:'',
				userWx:'',

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    name: [
                        { required: true, message: '请输入银商名称', trigger: 'blur' }
                    ],
                    qq: [
                        { required: true, message: '请输入银商QQ号', trigger: 'blur' }
                    ],
                    wx: [
                        { required: true, message: '请输入银商微信号', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
                    name:'',
                    qq:'',
                    wx:'',
                    friendliness:'1'
				}

			}
		},
		methods: {
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers(this.pageSize);
			},
			//获取用户列表
			getUsers(page) {
				this.listLoading = true;
                this.$axios.get('/gold-merchants?name='+this.username+'&qq='+this.userqq+'&wx='+this.userWx+'&index='+this.page+'&size='+page,{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.vipList = res.data.data
                        this.total = res.data.count
                        this.listLoading = false;
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
			//删除
			handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$axios.delete('/gold-merchant/'+row.id,{},this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '删除成功', type: 'success'});
                            this.getUsers(this.pageSize);
                            this.listLoading = false
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
                }).catch(() => {

                });
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    name:'',
                    qq:'',
                    wx:'',
                    friendliness:'1'
				};
			},
			//新增
			addSubmit: function () {
			    if (this.addForm.name == ''){
                    this.$message({message: '请输入银商名称', type: 'error'});
				}else if(this.addForm.qq == ''){
                    this.$message({message: '请输入银商QQ', type: 'error'});
				}else if(this.addForm.wx == ''){
                    this.$message({message: '请输入银商微信', type: 'error'});
                }else{
                    this.addForm.friendliness = parseInt(this.addForm.friendliness)
                    this.$axios.post('/gold-merchant',this.addForm,this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '添加成功', type: 'success'});
                            this.addFormVisible = false;
                            this.getUsers(this.pageSize);
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
				}
			},
		},
		mounted() {
			this.getUsers(this.pageSize);
		}
	}

</script>

<style scoped>

</style>